package com.wanxi.company.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.wanxi.common.entity.BusinessLineModel;
import com.wanxi.common.entity.BusinessModel;
import com.wanxi.common.result.ResultDTO;
import com.wanxi.company.mapper.BusinessMapper;
import com.wanxi.company.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class BusinessServiceImpl implements BusinessService {


    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public ResultDTO add(BusinessModel businessModel) {
        LambdaQueryWrapper<BusinessModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(BusinessModel::getId)
                .eq(businessModel.getName() != null, BusinessModel::getName, businessModel.getName());
        boolean exists = businessMapper.exists(queryWrapper);
        if (exists) {
            return ResultDTO.builder()
                    .code(0)
                    .msg("same name")
                    .count(-1)
                    .data("")
                    .build();
        } else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("")
                    .count(businessMapper.insert(businessModel))
                    .data("")
                    .build();
        }
    }

    @Override
    public ResultDTO update(BusinessModel businessModel) {
        LambdaQueryWrapper<BusinessModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(BusinessModel::getId)
                .eq(businessModel.getName() != null, BusinessModel::getName, businessModel.getName());
        boolean exists = businessMapper.exists(queryWrapper);
        if (exists) {
            return ResultDTO.builder()
                    .code(0)
                    .msg("")
                    .count(businessMapper.updateById(businessModel))
                    .data("")
                    .build();
        } else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("have no")
                    .count(-1)
                    .data("")
                    .build();
        }
    }

    @Override
    public ResultDTO delete(BusinessModel businessModel) {
        return ResultDTO.builder()
                .code(0)
                .msg("")
                .count(businessMapper.deleteById(businessModel))
                .data("")
                .build();
    }

    @Override
    public ResultDTO findById(BusinessModel businessModel) {
        LambdaQueryWrapper<BusinessModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(BusinessModel::getId, BusinessModel::getName, BusinessModel::getImgHref,
                        BusinessModel::getContent, BusinessModel::getBusinessLineId, BusinessModel::getEnable,
                        BusinessModel::getIsRecommend, BusinessModel::getIsShow, BusinessModel::getMoney)
                .eq(businessModel.getId() != null, BusinessModel::getId, businessModel.getId());
        BusinessModel business = businessMapper.selectOne(queryWrapper);
        return ResultDTO.builder()
                .code(0)
                .msg("")
                .data(business)
                .build();
    }

    @Override
    public ResultDTO findAll(BusinessModel businessModel) {

        MPJLambdaWrapper<BusinessModel> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(BusinessModel::getId, BusinessModel::getName, BusinessModel::getImgHref,
                        BusinessModel::getContent, BusinessModel::getBusinessLineId, BusinessModel::getEnable,
                        BusinessModel::getIsRecommend, BusinessModel::getIsShow, BusinessModel::getMoney)
                .selectAs(BusinessLineModel::getName,BusinessModel::getBusinessLineName)
                .ge(businessModel.getStartMoney() != null, BusinessModel::getMoney, businessModel.getStartMoney())
                .le(businessModel.getEndMoney() != null, BusinessModel::getMoney, businessModel.getEndMoney())
                .like(businessModel.getName() != null && businessModel.getName() != "", BusinessModel::getName, businessModel.getName())
                .like(businessModel.getContent() != null && businessModel.getContent() != "", BusinessModel::getContent, businessModel.getContent())
                .eq(businessModel.getEnable()!=null && businessModel.getEnable() != -1,BusinessModel::getEnable,businessModel.getEnable())
                .eq(businessModel.getIsRecommend()!=null && businessModel.getIsRecommend() != -1,BusinessModel::getIsRecommend,businessModel.getIsRecommend())
                .eq(businessModel.getIsShow()!=null && businessModel.getIsShow() != -1,BusinessModel::getIsShow,businessModel.getIsShow())
                .eq(businessModel.getBusinessLineId() != null && businessModel.getBusinessLineId() != 0, BusinessModel::getBusinessLineId, businessModel.getBusinessLineId())
                .leftJoin(BusinessLineModel.class,BusinessLineModel::getId,BusinessModel::getBusinessLineId);

        IPage<BusinessModel> page= new Page<>(businessModel.getPage(),businessModel.getLimit());
        IPage<BusinessModel> result = businessMapper.selectPage(page, wrapper);

        return ResultDTO.builder()
                .code(0)
                .msg("")
                .count((int) result.getTotal())
                .data(result.getRecords())
                .build();
    }

}
